package com.moli.iov.open.web.controller;

import com.moli.iov.base.model.dto.req.QueryVehicleFenceOpDto;
import com.moli.iov.base.model.dto.req.VehicleFenceReqOpDto;
import com.moli.iov.base.model.dto.res.VehicleFenceGpsResDto;
import com.moli.iov.open.model.vo.req.QueryVehicleFenceOpVo;
import com.moli.iov.open.model.vo.req.VehicleFenceVo;
import com.moli.iov.open.service.VehicleFenceService;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 车辆监控 > 电子围栏
 *
 * @author whl
 */
@Slf4j
@RequestMapping("/api/vehicleFence/")
@RestController
public class VehicleFenceController extends BaseController {

  @Autowired
  private VehicleFenceService vehicleFenceService;


  /**
   * 添加围栏
   */
  @PostMapping("/save")
  public RestResponse saveVehicleFence(
      @Validated({VehicleFenceVo.Save.class}) @RequestBody VehicleFenceVo vo) {
    assembleUserInfo(vo);
    VehicleFenceReqOpDto dto = new VehicleFenceReqOpDto();
    CommonUtils.copyProperties(vo, dto);
    dto.setFenceName(dto.getFenceName()+"围栏");
    VehicleFenceGpsResDto resDto = new VehicleFenceGpsResDto();
    resDto.setLatitude(vo.getLatitude());
    resDto.setLongitude(vo.getLongitude());
    dto.setFenceAreaCoordinate(resDto);
    return vehicleFenceService.saveVehicleFence(dto);
  }

  /**
   * 修改围栏
   */
  @PostMapping("/update")
  public RestResponse updateVehicleFence(
      @Validated({VehicleFenceVo.Update.class}) @RequestBody VehicleFenceVo vo) {
    assembleUserInfo(vo);
    VehicleFenceReqOpDto dto = new VehicleFenceReqOpDto();
    CommonUtils.copyProperties(vo, dto);
    dto.setFenceName(dto.getFenceName()+"围栏");
    VehicleFenceGpsResDto resDto = new VehicleFenceGpsResDto();
    resDto.setLatitude(vo.getLatitude());
    resDto.setLongitude(vo.getLongitude());
    dto.setFenceAreaCoordinate(resDto);
    return vehicleFenceService.updateVehicleFence(dto);
  }

  /**
   * 删除围栏
   */
  @PostMapping("/delete")
  public RestResponse deleteVehicleFence(
      @Validated({VehicleFenceVo.Delete.class}) @RequestBody VehicleFenceVo vo) {
    assembleUserInfo(vo);
    VehicleFenceReqOpDto dto = new VehicleFenceReqOpDto();
    CommonUtils.copyProperties(vo, dto);
    return vehicleFenceService.deleteVehicleFence(dto);
  }

  /**
   * 3.10 所有围栏接口
   *
   * @param vo  .getFenceIdList the fence ids 可以为空
   * @return the rest response
   * @author wanghualin
   */
  @PostMapping("/list")
  public RestResponse listVehicleFence( @RequestBody QueryVehicleFenceOpVo vo){
    log.info("url:"+request.getContextPath()+":所有围栏接口:{}", vo);
    assembleUserInfo(vo);
    QueryVehicleFenceOpDto dto = new QueryVehicleFenceOpDto();
    CommonUtils.copyProperties(vo, dto);
    return vehicleFenceService.listVehicleFences(dto);
  }

  /**
   * 3.10 当前围栏是否绑定有车
   *
   * @param vo the fence ids 可以为空
   * @return the rest response
   * @author wanghualin
   */
  @PostMapping("/isBindingVehicleByFenceId")
  public RestResponse isBindingVehicleByFenceId( @RequestBody  QueryVehicleFenceOpVo vo){
    log.info("url:"+request.getContextPath()+":当前围栏是否绑定有车入参:{}", vo);
    assembleUserInfo(vo);
    QueryVehicleFenceOpDto dto = new QueryVehicleFenceOpDto();
    CommonUtils.copyProperties(vo, dto);
    return vehicleFenceService.isBindingVehicleByFenceId(dto);
  }

  /**
   * 3.10 根据围栏名称模糊查找围栏
   *
   * @param vo fenceName 可以为空
   * @return the rest response
   * @author wanghualin
   */
  @PostMapping("/listByFenceName")
  public RestResponse listVehicleFenceByFenceName(  @RequestBody QueryVehicleFenceOpVo vo){
    log.info("url:"+request.getContextPath()+":根据围栏名称模糊查找围栏入参:{}", vo);
    assembleUserInfo(vo);
    QueryVehicleFenceOpDto dto = new QueryVehicleFenceOpDto();
    CommonUtils.copyProperties(vo, dto);
    return vehicleFenceService.listVehicleFenceByFenceName(dto);
  }
  /**
   * 3.10 根据vinList返回围栏信息
   *
   * @param vo .vinlist 车架号list
   * @return the rest response
   * @author wanghualin
   */
  @PostMapping("/listByVinList")
  public RestResponse listVehicleFenceByVinList(  @RequestBody QueryVehicleFenceOpVo vo){
    assembleUserInfo(vo);
    log.info("url:"+request.getContextPath()+":根据车架号返回围栏信息入参:{}", vo);
    QueryVehicleFenceOpDto dto = new QueryVehicleFenceOpDto();
    CommonUtils.copyProperties(vo, dto);
    return vehicleFenceService.listVehicleFenceByVinList(dto);
  }
  /**
   * 3.10 根据vinList返回围栏信息
   *
   * @param vo .vinlist 车架号list
   * @return the rest response
   * @author wanghualin
   */
  @PostMapping("/getVehicleFenceById")
  public RestResponse getVehicleFenceById(  @RequestBody QueryVehicleFenceOpVo vo, HttpServletRequest  request){
    assembleUserInfo(vo);
    log.info("url:"+request.getContextPath()+":根据围栏id获取围栏入参:{}", vo);
    QueryVehicleFenceOpDto dto = new QueryVehicleFenceOpDto();
    CommonUtils.copyProperties(vo, dto);
    return vehicleFenceService.getVehicleFenceById(dto);
  }

}
